package com.financialsharing.controller;

import com.auth0.jwt.internal.org.apache.commons.io.IOUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.financialsharing.common.ServerResponse;
import com.financialsharing.common.UserUtil;
import com.financialsharing.pojo.experiment.Experiment;
import com.financialsharing.pojo.experiment.ExperimentReportVo;
import com.financialsharing.service.ExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author lvyongwei
 */
@RestController
@RequestMapping("/experiment")
public class ExperimentController {

    @Autowired
    private ExperimentService experimentService;

    /**
     * 提交成绩
     *
     * @param experiment
     * @param request
     * @return
     */
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Experiment experiment, HttpServletRequest request) {
        try {
            experiment.setUserId(UserUtil.getUserId(request));
            boolean flag = experimentService.insertExperiment(experiment);
            if (flag == false) {
                return ServerResponse.createByErrorMsg("提交内容失败");
            }
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 修改提交内容
     *
     * @param experiment
     * @return
     */
    @PostMapping("/update")
    public ServerResponse update(@RequestBody Experiment experiment) {
        try {
            experimentService.updateExperiment(experiment);
            return ServerResponse.createBySuccessMsg("修改成绩成功");
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("修改成绩失败");
        }
    }

    /**
     * 查看实验报告列表
     *
     * @param experimentReportVo
     * @param request
     * @return
     */
    @PostMapping("/getReport")
    public ServerResponse getReport(@RequestBody ExperimentReportVo experimentReportVo, HttpServletRequest request) {
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        try {
            //判断不是管理员和教师身份
            if (!roleId.equals(1) && !roleId.equals(2)) {
                PageHelper.startPage(experimentReportVo.getPageNum(), experimentReportVo.getPageSize());
                List<ExperimentReportVo> allReport = experimentService.getAllReport(userId, experimentReportVo.getMessage());
                PageInfo<ExperimentReportVo> pageInfo = new PageInfo<ExperimentReportVo>(allReport);
                return ServerResponse.createBySuccess("获取实验报告成功", pageInfo);
            }
            PageHelper.startPage(experimentReportVo.getPageNum(), experimentReportVo.getPageSize());
            List<ExperimentReportVo> allReport = experimentService.getAllReport(null, experimentReportVo.getMessage());
            PageInfo<ExperimentReportVo> pageInfo = new PageInfo<ExperimentReportVo>(allReport);
            return ServerResponse.createBySuccess("获取实验报告成功", pageInfo);
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("获取实验报告失败");
        }
    }


    @RequestMapping(value = "/downlode",method = RequestMethod.GET)
    public ResponseEntity downlode() throws IOException  {
        //File file = new File("C:\\webProject\\Java\\项目操作说明文档.pdf");
        File file = new File("C:\\Users\\Administrator\\Desktop\\项目操作说明文档.pdf");

        InputStream in = new FileInputStream(file);
        final HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/pdf");
        headers.add("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("utf-8"),"ISO8859-1"));
        return new ResponseEntity<>(IOUtils.toByteArray(in), headers, HttpStatus.OK);

    }

}